package cn.hebutrun.oulabot.controller;

import cn.hebutrun.oulabot.common.Constants;
import cn.hebutrun.oulabot.common.aop.NotControllerResponseAdvice;
import cn.hebutrun.oulabot.common.util.TimeUtil;
import cn.hebutrun.oulabot.entity.LunchInfo;
import cn.hebutrun.oulabot.service.ILunchInfoService;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * 午餐信息表 前端控制器
 *
 * @author zhaolong
 * @since 2022-09-07
 */
@Api(tags = "午饭接口")
@RestController
@RequestMapping("/lunchInfo")
@RequiredArgsConstructor
public class LunchInfoController {

  private final ILunchInfoService iLunchInfoService;

  @GetMapping
  @ApiOperation("获取午饭列表-网页版")
  @NotControllerResponseAdvice
  public String lunchInfoPage(@RequestHeader("x-real-ip") String remoteAddr) {

    iLunchInfoService.today(remoteAddr);

    List<LunchInfo> lunchInfoList = iLunchInfoService.lunchInfo(remoteAddr);
    StringBuilder resStr = new StringBuilder("IP:" + remoteAddr + "\n");
    lunchInfoList.forEach(
        x ->
            resStr
                .append(x.getContent())
                .append("   [")
                .append(LocalDateTimeUtil.format(x.getCreateTime(), "yyyy年MM月dd日"))
                .append("   ")
                .append(
                    TimeUtil.dayOfWeek(
                        Date.from(x.getCreateTime().atZone(ZoneId.systemDefault()).toInstant())))
                .append("]\n"));
    return StrUtil.format(Constants.HTML_CONTENT_TEXT, resStr);
  }

  @GetMapping("/lunchInfo")
  public List<LunchInfo> lunchInfo(HttpServletRequest httpServletRequest) {
    return iLunchInfoService.lunchInfo(httpServletRequest.getRemoteAddr());
  }

  @GetMapping("/today")
  public String today(@RequestParam String QQ) {
    return iLunchInfoService.today(QQ);
  }

  @GetMapping("/lunchList")
  public String lunchList(@RequestParam String qq) {
    return iLunchInfoService.lunchListInfo(qq);
  }
}
